module	sys_detect_pll(
	input	wire		sclk,
	input	wire		resetb,
	input	wire		key_input,
	input	wire		clk_ok_n,
	output	reg		tx_err_en,
	input	wire		init_end,
	input	wire		time_1ms_sync,
	output	reg		pll_reset
);


//**************************************************************
//Dedect DCM unlocked option
//**************************************************************
parameter	TEST_PUSH_KEY_RST_DCM_ENABLE		=1'b0;
parameter	DCM_UNLOCKED_AUTO_RST_DCM_ENABLE	=1'b0;
parameter	DCM_UNLOCKED_SEND_ERROR_CRC_ENABLE	=1'b1;
parameter	LEDR_FLASH_NORMAL_ENABLE		=1'b0;//R AND G LED SYNC FLASH IS NORMAL MODE;ASYNC IS

//reg		pll_reset;
reg	[2:0]	reset_dcm_cnt;
reg		reset_dcm_flag;
reg		reset_dcm_150;
reg		init_end_flag=0;
reg		clk_ok_n_t;
always @*
	if(TEST_PUSH_KEY_RST_DCM_ENABLE==1'd1 && DCM_UNLOCKED_AUTO_RST_DCM_ENABLE==1'd1)
		pll_reset<=(~key_input)|reset_dcm_150;
	else if(DCM_UNLOCKED_AUTO_RST_DCM_ENABLE==1'd1)
		pll_reset<=reset_dcm_150;
	else if(TEST_PUSH_KEY_RST_DCM_ENABLE==1'd1)
		pll_reset<=~key_input;
	else pll_reset<='d0;


always @(posedge sclk or negedge resetb)
	if(!resetb)
		reset_dcm_flag<='d0;
	else if(init_end_flag=='d1)
	begin
		if(reset_dcm_flag==0 && clk_ok_n=='d1)
			reset_dcm_flag<='d1;
		else if (reset_dcm_cnt=='d4)
			reset_dcm_flag<='d0;
	end
	else	reset_dcm_flag<='d0;
		

always @(posedge sclk)
	if(reset_dcm_flag==0)
		reset_dcm_cnt<=0;
	else if(time_1ms_sync==1)
		reset_dcm_cnt<=reset_dcm_cnt+1;

always @(posedge sclk)
	if(reset_dcm_cnt=='d1)
		reset_dcm_150<='d1;
	else reset_dcm_150<='d0;
always @(posedge sclk or negedge resetb)
	if(resetb==0)
		tx_err_en<=0;
	else if(clk_ok_n=='d1 && init_end_flag && DCM_UNLOCKED_SEND_ERROR_CRC_ENABLE==1'd1)
		tx_err_en<='d1;
		
always @(posedge sclk  or negedge resetb)
	if(resetb==0)
		init_end_flag<=0;
	else if(init_end)
		init_end_flag<='d1;
endmodule